package com.jsmtr.rms.utils;

import java.util.Calendar;
import java.util.regex.Pattern;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2018/10/26 9:23
 * @modified by:
 */
public class CardNoTools {

    /**
     * 通过身份证号获取年龄
     * @param cardno 身份证号
     * @return
     * @throws Exception
     */
    public static  String getAge(String cardno) throws Exception{

        int idxSexStart = 16;
        int birthYearSpan = 4;
        //如果是15位的证件号码
        if(cardno.length() == 15) {
            idxSexStart = 14;
            birthYearSpan = 2;
        }

        //出生日期
        String year = (birthYearSpan == 2 ? "19" : "") + cardno.substring(6, 6 + birthYearSpan);
        String month = cardno.substring(6 + birthYearSpan, 6 + birthYearSpan + 2);
        String day = cardno.substring(8 + birthYearSpan, 8 + birthYearSpan + 2);


        //年龄
        Calendar certificateCal = Calendar.getInstance();
        Calendar currentTimeCal = Calendar.getInstance();
        certificateCal.set(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day));
        int yearAge = (currentTimeCal.get(Calendar.YEAR)) - (certificateCal.get(Calendar.YEAR));
        certificateCal.set(currentTimeCal.get(Calendar.YEAR), Integer.parseInt(month)-1, Integer.parseInt(day));
        int monthFloor = (currentTimeCal.before(certificateCal) ? 1 : 0);
        int age=yearAge-monthFloor;
        return  String.valueOf(age);
    }

    /**
     * 根据身份证号获取性别
     * @param cardno 身份证号
     * @return 男,女
     * @throws Exception
     */
    public static String getGender(String cardno) throws Exception{
        int idxSexStart = 16;
        //如果是15位的证件号码
        if(cardno.length() == 15) {
            idxSexStart = 14;
        }
        //性别
        String idxSexStr = cardno.substring(idxSexStart, idxSexStart + 1);
        int idxSex = Integer.parseInt(idxSexStr) % 2;
        String sex = (idxSex == 1) ? "男" : "女";
        return sex;

    }

    /**
     * 根据身份证号获取出生日期
     * @param cardno 身份证号
     * @return YYYY-MM-DD
     * @throws Exception
     */
    public static String getBirthday(String cardno) throws Exception{
        String myRegExpIDCardNo = "^\\d{6}(((19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\\d{3}([0-9]|x|X))|(\\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\\d{3}))$";
        boolean valid= Pattern.matches(myRegExpIDCardNo,cardno)||(cardno.length() == 17 && Pattern.matches(myRegExpIDCardNo,cardno.substring(0,15)));
        if(!valid){
            throw  new Exception("证号号码不规范!");
        }
        int idxSexStart = 16;
        int birthYearSpan = 4;
        //如果是15位的证件号码
        if(cardno.length() == 15) {
            idxSexStart = 14;
            birthYearSpan = 2;
        }

        String year = (birthYearSpan == 2 ? "19" : "") + cardno.substring(6, 6 + birthYearSpan);
        String month = cardno.substring(6 + birthYearSpan, 6 + birthYearSpan + 2);
        String day = cardno.substring(8 + birthYearSpan, 8 + birthYearSpan + 2);
        String birthday = year + '-' + month + '-' + day;
        return birthday;

    }


}
